/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.app;

import com.railway.utils.CustomTableModel;
import com.railway.classes.Route;
import com.railway.classes.Station;
import com.railway.utils.CustomScrollBarUI;
import com.railway.utils.MapSortHelper;
import com.railway.utils.ReorderableJList;
import com.railway.utils.RestServer;
import com.railway.utils.Toast;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.SpinnerDateModel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Kenny
 */
public class RoutePanel extends javax.swing.JPanel {

    Image imgArrow = new ImageIcon(this.getClass().getResource("/com/railway/image/arrow.png")).getImage();
    Image imgArrow270 = new ImageIcon(this.getClass().getResource("/com/railway/image/arrow-270.png")).getImage();
    private ArrayList<String> colName;
    private ArrayList<ArrayList<Object>> rowData;
    private ArrayList<Map<String, String>> stationTimes = new ArrayList<>();
    private Map<String, String> stationTime;
    private CustomTableModel ctmModel;
    private DefaultListModel lstModel;
    private DefaultListModel<Station> lstStationModel;
    private DefaultListModel<Station> lstAddModel;
    private DefaultListModel<Station> dlmROS;
    private DefaultListModel<Station> dlmSIR;
    private int selectedIndex;
    private JSONObject json;
    private JSONArray jsonArray;
    private CardLayout cl;
    private Calendar c = Calendar.getInstance();
    private Date initDate;
    private Date maxDate;
    private String status = "ACV";

    /**
     * Creates new form RoutePanel
     */
    public RoutePanel() {
        c.set(1970, 0, 1, 0, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        initDate = c.getTime();
        c.set(1970, 0, 1, 23, 59, 0);
        c.set(Calendar.MILLISECOND, 0);
        maxDate = c.getTime();
        initComponents();
        jspTableScroller.getViewport().setBackground(Color.WHITE);
        populateTableData();
        cl = (CardLayout) jpRouteCard.getLayout();
    }

    private void populateTableData() {
        new Thread() {
            @Override
            public void run() {
                colName = new ArrayList<>();
                colName.add("Route ID");
                colName.add("Route Name");
                colName.add("Status");
                String data = RestServer.httpGet("route", "");
                try {
                    json = new JSONObject(data);
                    boolean error = json.optBoolean("error");
                    if (!error) {
                        int status = json.getInt("status");
                        if (status == 200) {
                            rowData = new ArrayList<>();
                            jsonArray = json.getJSONArray("content");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                ArrayList<Object> temp = new ArrayList<>();
                                temp.add(jsonArray.getJSONObject(i).getInt("rid"));
                                temp.add(jsonArray.getJSONObject(i).getString("routeName"));
                                temp.add((jsonArray.getJSONObject(i).getString("status").equals("ACV")) ? "Active" : "Inactive");
                                rowData.add(temp);
                            }
                            ctmModel = new CustomTableModel(colName, rowData);
                            jtbRoute.setModel(ctmModel);
                            jtbRoute.removeColumn(jtbRoute.getColumnModel().getColumn(0));
                        }
                    } else {
                        Toast.makeText(RoutePanel.this, json.getString("message"), Toast.LENGTH_LONG).display();
                    }
                } catch (JSONException ex) {
                }
            }
        }.start();
    }

    private void populateAvailableStation() {
        new Thread() {
            @Override
            public void run() {
                lstStationModel = new DefaultListModel<>();
                lstAddModel = new DefaultListModel<>();
                String result = RestServer.httpGet("station/active", "");
                try {
                    json = new JSONObject(result);
                    boolean error = json.optBoolean("error");
                    if (!error) {
                        int status = json.getInt("status");
                        if (status == 200) {
                            jsonArray = json.getJSONArray("content");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                lstStationModel.addElement(
                                        new Station(i,
                                        jsonArray.getJSONObject(i).getInt("sid"),
                                        jsonArray.getJSONObject(i).getString("stationName")));
                            }
                            jltAvailableStation.setModel(lstStationModel);
                            jltStationToAdd.setModel(lstAddModel);
                        }
                    } else {
                        Toast.makeText(RoutePanel.this, json.getString("message"), Toast.LENGTH_LONG).display();
                    }
                } catch (JSONException ex) {
                }
            }
        }.start();
    }

    private String buildJson(ArrayList<Map<String, String>> s, int sorting) {
        JSONObject jsonObj;
        JSONArray jsonArr = new JSONArray();
        Collections.sort(s, new MapSortHelper(sorting));
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        boolean desc = false;
        Date d1;
        Date d2;
        String travelPeriod = null;
        if (sorting == MapSortHelper.SORT_DSC) {
            desc = true;
            travelPeriod = s.get(0).get("time");
        }
        for (int i = 0; i < s.size(); i++) {
            try {
                jsonObj = new JSONObject();
                Map<String, String> map = s.get(i);
                jsonObj.put("sid", map.get("sid"));
                if (desc) {
                    d1 = format.parse(travelPeriod);
                    d2 = format.parse(map.get("time"));
                    c.set(1970, 0, 1, 0, 0, 0);
                    c.add(Calendar.MILLISECOND, (int) (d1.getTime() - d2.getTime()));
                    String time = format.format(c.getTime());
                    jsonObj.put("time", time);
                } else {
                    jsonObj.put("time", map.get("time"));
                }
                jsonArr.put(jsonObj);
            } catch (JSONException | ParseException ex) {
            }
        }
        return jsonArr.toString();
    }

    private String buildJson(ArrayList<Map<String, String>> s) {
        JSONObject jsonObj;
        JSONArray jsonArr = new JSONArray();
        for (int i = 0; i < s.size(); i++) {
            try {
                jsonObj = new JSONObject();
                Map<String, String> map = s.get(i);
                jsonObj.put("sid", map.get("sid"));
                jsonObj.put("time", map.get("time"));
                jsonArr.put(jsonObj);
            } catch (JSONException ex) {
            }
        }
        return jsonArr.toString();
    }

    private void resetTimeEditing() {
        jtfStationTime.setText(null);
        jspArrivalTime.setValue(initDate);
        jbtSetTime.setEnabled(true);
    }

    private void populateRouteComboBox() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                DefaultComboBoxModel<Route> dcbmRoute = new DefaultComboBoxModel<>();
                String result = RestServer.httpGet("routes", "");
                try {
                    json = new JSONObject(result);
                    boolean error = json.optBoolean("error");
                    if (!error) {
                        int statuscode = json.getInt("status");
                        if (statuscode == 200) {
                            jsonArray = json.getJSONArray("content");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                Route temp = new Route();
                                temp.setRid(jsonArray.getJSONObject(i).getInt("rid"));
                                temp.setRouteName(jsonArray.getJSONObject(i).getString("routeName"));
                                temp.setStatus(jsonArray.getJSONObject(i).getString("status"));
                                dcbmRoute.addElement(temp);
                            }
                            jcbxRoute.setModel(dcbmRoute);
                        } else {
                            Toast.makeText(RoutePanel.this, "No active route found.", Toast.LENGTH_LONG).display();
                        }
                    } else {
                        Toast.makeText(RoutePanel.this, json.getString("message"), Toast.LENGTH_LONG).display();
                    }
                } catch (JSONException ex) {
                }
            }
        }).start();
    }

    private void populateStationInRoute(final int rid) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                dlmSIR = new DefaultListModel<>();
                String result = RestServer.httpGet("route/station", "" + rid);
                try {
                    json = new JSONObject(result);
                    boolean error = json.optBoolean("error");
                    if (!error) {
                        int statusCode = json.getInt("status");
                        if (statusCode == 200) {
                            jsonArray = json.getJSONArray("content");
                            lstModel = new DefaultListModel();
                            for (int i = 0; i < jsonArray.length(); i++) {
                                Station temp = new Station();
                                temp.setOrder(i);
                                temp.setSid(jsonArray.getJSONObject(i).getInt("sid"));
                                temp.setStationName(jsonArray.getJSONObject(i).getString("stationName"));
                                dlmSIR.addElement(temp);
                            }
                            jltStationinRoute.setModel(dlmSIR);
                        }
                    } else {
                        Toast.makeText(RoutePanel.this, json.getString("message"), Toast.LENGTH_LONG).display();
                    }
                } catch (JSONException ex) {
                }
            }
        }).start();
    }

    private void populateRestOfStation(final int rid) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                dlmROS = new DefaultListModel<>();
                String result = RestServer.httpGet("station/notin", "" + rid);
                try {
                    json = new JSONObject(result);
                    boolean error = json.optBoolean("error");
                    if (!error) {
                        int statusCode = json.getInt("status");
                        if (statusCode == 200) {
                            jsonArray = json.getJSONArray("content");
                            lstModel = new DefaultListModel();
                            for (int i = 0; i < jsonArray.length(); i++) {
                                Station temp = new Station();
                                temp.setOrder(i);
                                temp.setSid(jsonArray.getJSONObject(i).getInt("sid"));
                                temp.setStationName(jsonArray.getJSONObject(i).getString("stationName"));
                                dlmROS.addElement(temp);
                            }
                            jltMAvaliableStation.setModel(dlmROS);
                        }
                    } else {
                        Toast.makeText(RoutePanel.this, json.getString("message"), Toast.LENGTH_LONG).display();
                    }
                } catch (JSONException ex) {
                }
            }
        }).start();
    }
    
    private void modifyReset(){
        jtfStationTime1.setText(null);
        jcbxRoute.setSelectedIndex(0);
        jrbMRouteActive.setSelected(true);
        jspArrivalTime1.setValue(initDate);
        jpStationTime1.setVisible(false);
        if (stationTime != null) {
            stationTime.clear();
        }
        if (!stationTimes.isEmpty()) {
            stationTimes.clear();
        }
        jbtStartSetTime1.setEnabled(true);
        jbtSetTime1.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbgAStatus = new javax.swing.ButtonGroup();
        jbgMStatus = new javax.swing.ButtonGroup();
        jpRoute = new javax.swing.JPanel();
        jpRouteContent = new javax.swing.JPanel();
        jspTableScroller = new javax.swing.JScrollPane();
        jtbRoute = new javax.swing.JTable();
        jpRouteOperation = new javax.swing.JPanel();
        jbtAddRoute = new javax.swing.JButton();
        jbtModifyRoute = new javax.swing.JButton();
        jbtRemoveRoute = new javax.swing.JButton();
        jpRouteCard = new javax.swing.JPanel();
        jpNullCard = new javax.swing.JPanel();
        jpAddRouteCard = new javax.swing.JPanel();
        jpAddArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow, 0, 0, getWidth(), imgArrow.getHeight(this), this);
            }
        };
        jpAddPanel = new javax.swing.JPanel();
        jlblARouteName = new javax.swing.JLabel();
        jtfARouteName = new javax.swing.JTextField();
        jlblARouteStatus = new javax.swing.JLabel();
        jrbARouteActive = new javax.swing.JRadioButton();
        jrbARouteInactive = new javax.swing.JRadioButton();
        jlblARouteError = new javax.swing.JLabel();
        jlblARouteStatus1 = new javax.swing.JLabel();
        jspAvaliableScroller = new javax.swing.JScrollPane();
        jltAvailableStation = new javax.swing.JList();
        jspStationToAdd = new javax.swing.JScrollPane();
        jltStationToAdd = new javax.swing.JList();
        jSeparator1 = new javax.swing.JSeparator();
        jbtAddStation = new javax.swing.JButton();
        jbtRemoveStation = new javax.swing.JButton();
        jbtStartSetTime = new javax.swing.JButton();
        jbtReset = new javax.swing.JButton();
        jbtSubmit = new javax.swing.JButton();
        jpStationTime = new javax.swing.JPanel();
        jpVerticalArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow270, 0, 0, imgArrow270.getWidth(this), getHeight(), this);
            }
        };
        jpEditStationTime = new javax.swing.JPanel();
        jlblSetTime = new javax.swing.JLabel();
        jtfStationTime = new javax.swing.JTextField();
        jlblArrivalTime = new javax.swing.JLabel();
        jspArrivalTime = new javax.swing.JSpinner(
            new SpinnerDateModel(initDate, null, maxDate, Calendar.MINUTE)
        );
        jbtSetTime = new javax.swing.JButton();
        jpModifyRouteCard = new javax.swing.JPanel();
        jpModifyArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow, 0, 0, getWidth(), imgArrow.getHeight(this), this);
            }
        };
        jpModifyPanel = new javax.swing.JPanel();
        jpStationTime1 = new javax.swing.JPanel();
        jpVerticalArrow1 = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow270, 0, 0, imgArrow270.getWidth(this), getHeight(), this);
            }
        };
        jpEditStationTime1 = new javax.swing.JPanel();
        jlblSetTime1 = new javax.swing.JLabel();
        jtfStationTime1 = new javax.swing.JTextField();
        jlblArrivalTime1 = new javax.swing.JLabel();
        jspArrivalTime1 = new javax.swing.JSpinner(
            new SpinnerDateModel(initDate, null, maxDate, Calendar.MINUTE)
        );
        jbtSetTime1 = new javax.swing.JButton();
        jlblMRouteName = new javax.swing.JLabel();
        jlblMRouteStatus = new javax.swing.JLabel();
        jrbMRouteActive = new javax.swing.JRadioButton();
        jrbMRouteInactive = new javax.swing.JRadioButton();
        jlblARouteStatus3 = new javax.swing.JLabel();
        jSeparator4 = new javax.swing.JSeparator();
        jspStationInRoute = new javax.swing.JScrollPane();
        jltStationinRoute = new ReorderableJList();
        jbtMAddStation = new javax.swing.JButton();
        jbtMRemoveStation = new javax.swing.JButton();
        jspMAvaliableStation = new javax.swing.JScrollPane();
        jltMAvaliableStation = new javax.swing.JList();
        jcbxRoute = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jSeparator5 = new javax.swing.JSeparator();
        jbtStartSetTime1 = new javax.swing.JButton();
        jbtSubmit1 = new javax.swing.JButton();
        jbtReset1 = new javax.swing.JButton();
        jspListScroller = new javax.swing.JScrollPane();
        jltStationInRoute = new javax.swing.JList();
        jlblRoutes = new javax.swing.JLabel();
        jlblStationInRoute = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jpRouteArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow, 0, 0, getWidth(), getHeight(), this);
            }
        };

        setPreferredSize(new java.awt.Dimension(870, 460));
        setLayout(null);

        jpRoute.setBackground(new java.awt.Color(220, 220, 220));
        jpRoute.setLayout(null);

        jpRouteContent.setBackground(new java.awt.Color(255, 255, 255));
        jpRouteContent.setLayout(null);

        jspTableScroller.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        jtbRoute.setAutoCreateRowSorter(true);
        jtbRoute.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtbRoute.getTableHeader().setReorderingAllowed(false);
        jtbRoute.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbRouteMouseClicked(evt);
            }
        });
        jspTableScroller.setViewportView(jtbRoute);

        jpRouteContent.add(jspTableScroller);
        jspTableScroller.setBounds(625, 25, 230, 190);

        jpRouteOperation.setBackground(new java.awt.Color(255, 255, 255));
        jpRouteOperation.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jpRouteOperation.setLayout(null);

        jbtAddRoute.setBackground(new java.awt.Color(52, 73, 94));
        jbtAddRoute.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtAddRoute.setForeground(new java.awt.Color(255, 255, 255));
        jbtAddRoute.setText("Add Route");
        jbtAddRoute.setFocusPainted(false);
        jbtAddRoute.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAddRouteActionPerformed(evt);
            }
        });
        jpRouteOperation.add(jbtAddRoute);
        jbtAddRoute.setBounds(5, 5, 170, 50);

        jbtModifyRoute.setBackground(new java.awt.Color(52, 73, 94));
        jbtModifyRoute.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtModifyRoute.setForeground(new java.awt.Color(255, 255, 255));
        jbtModifyRoute.setText("Modify Route");
        jbtModifyRoute.setFocusPainted(false);
        jbtModifyRoute.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtModifyRouteActionPerformed(evt);
            }
        });
        jpRouteOperation.add(jbtModifyRoute);
        jbtModifyRoute.setBounds(225, 5, 170, 50);

        jbtRemoveRoute.setBackground(new java.awt.Color(52, 73, 94));
        jbtRemoveRoute.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtRemoveRoute.setForeground(new java.awt.Color(255, 255, 255));
        jbtRemoveRoute.setText("Deactivate Route");
        jbtRemoveRoute.setFocusPainted(false);
        jbtRemoveRoute.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtRemoveRouteActionPerformed(evt);
            }
        });
        jpRouteOperation.add(jbtRemoveRoute);
        jbtRemoveRoute.setBounds(440, 5, 170, 50);

        jpRouteCard.setBackground(new java.awt.Color(255, 255, 255));
        jpRouteCard.setLayout(new java.awt.CardLayout());

        jpNullCard.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jpNullCardLayout = new javax.swing.GroupLayout(jpNullCard);
        jpNullCard.setLayout(jpNullCardLayout);
        jpNullCardLayout.setHorizontalGroup(
            jpNullCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 605, Short.MAX_VALUE)
        );
        jpNullCardLayout.setVerticalGroup(
            jpNullCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 360, Short.MAX_VALUE)
        );

        jpRouteCard.add(jpNullCard, "null");

        jpAddRouteCard.setBackground(new java.awt.Color(255, 255, 255));
        jpAddRouteCard.setLayout(null);

        jpAddArrow.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jpAddArrowLayout = new javax.swing.GroupLayout(jpAddArrow);
        jpAddArrow.setLayout(jpAddArrowLayout);
        jpAddArrowLayout.setHorizontalGroup(
            jpAddArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 170, Short.MAX_VALUE)
        );
        jpAddArrowLayout.setVerticalGroup(
            jpAddArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        jpAddRouteCard.add(jpAddArrow);
        jpAddArrow.setBounds(0, 0, 170, 10);

        jpAddPanel.setBackground(new java.awt.Color(255, 255, 255));
        jpAddPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jpAddPanel.setLayout(null);

        jlblARouteName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblARouteName.setText("Route Name");
        jpAddPanel.add(jlblARouteName);
        jlblARouteName.setBounds(10, 20, 90, 30);
        jpAddPanel.add(jtfARouteName);
        jtfARouteName.setBounds(100, 20, 170, 30);

        jlblARouteStatus.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblARouteStatus.setText("Status");
        jpAddPanel.add(jlblARouteStatus);
        jlblARouteStatus.setBounds(330, 20, 70, 30);

        jrbARouteActive.setBackground(new java.awt.Color(255, 255, 255));
        jbgAStatus.add(jrbARouteActive);
        jrbARouteActive.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbARouteActive.setText("Active");
        jrbARouteActive.setActionCommand("ACV");
        jrbARouteActive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbARouteActiveActionPerformed(evt);
            }
        });
        jpAddPanel.add(jrbARouteActive);
        jrbARouteActive.setBounds(400, 20, 80, 30);

        jrbARouteInactive.setBackground(new java.awt.Color(255, 255, 255));
        jbgAStatus.add(jrbARouteInactive);
        jrbARouteInactive.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbARouteInactive.setText("Inactive");
        jrbARouteInactive.setActionCommand("INV");
        jrbARouteInactive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbARouteInactiveActionPerformed(evt);
            }
        });
        jpAddPanel.add(jrbARouteInactive);
        jrbARouteInactive.setBounds(490, 20, 80, 30);

        jlblARouteError.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jlblARouteError.setForeground(new java.awt.Color(255, 0, 0));
        jpAddPanel.add(jlblARouteError);
        jlblARouteError.setBounds(100, 50, 170, 10);

        jlblARouteStatus1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblARouteStatus1.setText("Add Station");
        jpAddPanel.add(jlblARouteStatus1);
        jlblARouteStatus1.setBounds(10, 60, 90, 30);

        jspAvaliableScroller.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        jltAvailableStation.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        jspAvaliableScroller.setViewportView(jltAvailableStation);

        jpAddPanel.add(jspAvaliableScroller);
        jspAvaliableScroller.setBounds(10, 95, 180, 180);

        jspStationToAdd.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        jltStationToAdd.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        jspStationToAdd.setViewportView(jltStationToAdd);

        jpAddPanel.add(jspStationToAdd);
        jspStationToAdd.setBounds(250, 95, 180, 180);
        jpAddPanel.add(jSeparator1);
        jSeparator1.setBounds(10, 90, 420, 5);

        jbtAddStation.setBackground(new java.awt.Color(26, 188, 156));
        jbtAddStation.setFont(new java.awt.Font("Segoe UI Emoji", 0, 11)); // NOI18N
        jbtAddStation.setForeground(new java.awt.Color(255, 255, 255));
        jbtAddStation.setText(">>");
        jbtAddStation.setFocusPainted(false);
        jbtAddStation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAddStationActionPerformed(evt);
            }
        });
        jpAddPanel.add(jbtAddStation);
        jbtAddStation.setBounds(195, 130, 50, 20);

        jbtRemoveStation.setBackground(new java.awt.Color(26, 188, 156));
        jbtRemoveStation.setFont(new java.awt.Font("Segoe UI Emoji", 0, 11)); // NOI18N
        jbtRemoveStation.setForeground(new java.awt.Color(255, 255, 255));
        jbtRemoveStation.setText("<<");
        jbtRemoveStation.setFocusPainted(false);
        jbtRemoveStation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtRemoveStationActionPerformed(evt);
            }
        });
        jpAddPanel.add(jbtRemoveStation);
        jbtRemoveStation.setBounds(195, 220, 50, 20);

        jbtStartSetTime.setBackground(new java.awt.Color(39, 174, 96));
        jbtStartSetTime.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtStartSetTime.setForeground(new java.awt.Color(255, 255, 255));
        jbtStartSetTime.setText("Set time");
        jbtStartSetTime.setEnabled(false);
        jbtStartSetTime.setFocusPainted(false);
        jbtStartSetTime.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtStartSetTimeActionPerformed(evt);
            }
        });
        jpAddPanel.add(jbtStartSetTime);
        jbtStartSetTime.setBounds(235, 290, 150, 45);

        jbtReset.setBackground(new java.awt.Color(39, 174, 96));
        jbtReset.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtReset.setForeground(new java.awt.Color(255, 255, 255));
        jbtReset.setText("Reset");
        jbtReset.setFocusPainted(false);
        jbtReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtResetActionPerformed(evt);
            }
        });
        jpAddPanel.add(jbtReset);
        jbtReset.setBounds(10, 290, 150, 45);

        jbtSubmit.setBackground(new java.awt.Color(39, 174, 96));
        jbtSubmit.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtSubmit.setForeground(new java.awt.Color(255, 255, 255));
        jbtSubmit.setText("Submit");
        jbtSubmit.setFocusPainted(false);
        jbtSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSubmitActionPerformed(evt);
            }
        });
        jpAddPanel.add(jbtSubmit);
        jbtSubmit.setBounds(450, 290, 150, 45);

        jpStationTime.setBackground(new java.awt.Color(255, 255, 255));
        jpStationTime.setLayout(null);

        jpVerticalArrow.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jpVerticalArrowLayout = new javax.swing.GroupLayout(jpVerticalArrow);
        jpVerticalArrow.setLayout(jpVerticalArrowLayout);
        jpVerticalArrowLayout.setHorizontalGroup(
            jpVerticalArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );
        jpVerticalArrowLayout.setVerticalGroup(
            jpVerticalArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 180, Short.MAX_VALUE)
        );

        jpStationTime.add(jpVerticalArrow);
        jpVerticalArrow.setBounds(0, 0, 10, 180);

        jpEditStationTime.setBackground(new java.awt.Color(255, 255, 255));
        jpEditStationTime.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jpEditStationTime.setLayout(null);

        jlblSetTime.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jlblSetTime.setText("Setting time for :");
        jpEditStationTime.add(jlblSetTime);
        jlblSetTime.setBounds(5, 5, 150, 15);

        jtfStationTime.setEditable(false);
        jtfStationTime.setBackground(new java.awt.Color(250, 250, 250));
        jpEditStationTime.add(jtfStationTime);
        jtfStationTime.setBounds(5, 25, 150, 20);

        jlblArrivalTime.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jlblArrivalTime.setText("<html>Time to arrive from first<br/>station : </html>");
        jpEditStationTime.add(jlblArrivalTime);
        jlblArrivalTime.setBounds(5, 50, 150, 30);

        JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(jspArrivalTime, "HH:mm");
        timeEditor.getTextField().setEditable(false);
        jspArrivalTime.setEditor(timeEditor);
        jpEditStationTime.add(jspArrivalTime);
        jspArrivalTime.setBounds(5, 90, 150, 20);

        jbtSetTime.setBackground(new java.awt.Color(26, 188, 156));
        jbtSetTime.setFont(new java.awt.Font("Segoe UI Emoji", 0, 11)); // NOI18N
        jbtSetTime.setForeground(new java.awt.Color(255, 255, 255));
        jbtSetTime.setText("Set");
        jbtSetTime.setFocusPainted(false);
        jbtSetTime.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSetTimeActionPerformed(evt);
            }
        });
        jpEditStationTime.add(jbtSetTime);
        jbtSetTime.setBounds(5, 120, 150, 20);

        jpStationTime.add(jpEditStationTime);
        jpEditStationTime.setBounds(10, 0, 160, 180);

        jpAddPanel.add(jpStationTime);
        jpStationTime.setBounds(430, 95, 170, 180);
        jpStationTime.setVisible(false);

        jpAddRouteCard.add(jpAddPanel);
        jpAddPanel.setBounds(0, 10, 605, 350);

        jpRouteCard.add(jpAddRouteCard, "addroute");

        jpModifyRouteCard.setBackground(new java.awt.Color(255, 255, 255));
        jpModifyRouteCard.setLayout(null);

        jpModifyArrow.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jpModifyArrowLayout = new javax.swing.GroupLayout(jpModifyArrow);
        jpModifyArrow.setLayout(jpModifyArrowLayout);
        jpModifyArrowLayout.setHorizontalGroup(
            jpModifyArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 170, Short.MAX_VALUE)
        );
        jpModifyArrowLayout.setVerticalGroup(
            jpModifyArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        jpModifyRouteCard.add(jpModifyArrow);
        jpModifyArrow.setBounds(220, 0, 170, 10);

        jpModifyPanel.setBackground(new java.awt.Color(255, 255, 255));
        jpModifyPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jpModifyPanel.setLayout(null);

        jpStationTime1.setBackground(new java.awt.Color(255, 255, 255));
        jpStationTime1.setLayout(null);

        jpVerticalArrow1.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jpVerticalArrow1Layout = new javax.swing.GroupLayout(jpVerticalArrow1);
        jpVerticalArrow1.setLayout(jpVerticalArrow1Layout);
        jpVerticalArrow1Layout.setHorizontalGroup(
            jpVerticalArrow1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );
        jpVerticalArrow1Layout.setVerticalGroup(
            jpVerticalArrow1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 180, Short.MAX_VALUE)
        );

        jpStationTime1.add(jpVerticalArrow1);
        jpVerticalArrow1.setBounds(0, 0, 10, 180);

        jpEditStationTime1.setBackground(new java.awt.Color(255, 255, 255));
        jpEditStationTime1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jpEditStationTime1.setLayout(null);

        jlblSetTime1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jlblSetTime1.setText("Setting time for :");
        jpEditStationTime1.add(jlblSetTime1);
        jlblSetTime1.setBounds(5, 5, 150, 15);

        jtfStationTime1.setEditable(false);
        jtfStationTime1.setBackground(new java.awt.Color(250, 250, 250));
        jpEditStationTime1.add(jtfStationTime1);
        jtfStationTime1.setBounds(5, 25, 150, 20);

        jlblArrivalTime1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jlblArrivalTime1.setText("<html>Time to arrive from first<br/>station : </html>");
        jpEditStationTime1.add(jlblArrivalTime1);
        jlblArrivalTime1.setBounds(5, 50, 150, 30);

        JSpinner.DateEditor mTimeEditor = new JSpinner.DateEditor(jspArrivalTime1, "HH:mm");
        mTimeEditor.getTextField().setEditable(false);
        jspArrivalTime1.setEditor(mTimeEditor);
        jspArrivalTime1.setEnabled(false);
        jpEditStationTime1.add(jspArrivalTime1);
        jspArrivalTime1.setBounds(5, 90, 150, 20);

        jbtSetTime1.setBackground(new java.awt.Color(26, 188, 156));
        jbtSetTime1.setFont(new java.awt.Font("Segoe UI Emoji", 0, 11)); // NOI18N
        jbtSetTime1.setForeground(new java.awt.Color(255, 255, 255));
        jbtSetTime1.setText("Set");
        jbtSetTime1.setEnabled(false);
        jbtSetTime1.setFocusPainted(false);
        jbtSetTime1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSetTime1ActionPerformed(evt);
            }
        });
        jpEditStationTime1.add(jbtSetTime1);
        jbtSetTime1.setBounds(5, 120, 150, 20);

        jpStationTime1.add(jpEditStationTime1);
        jpEditStationTime1.setBounds(10, 0, 160, 180);

        jpModifyPanel.add(jpStationTime1);
        jpStationTime1.setBounds(430, 95, 170, 180);
        jpStationTime1.setVisible(false);

        jlblMRouteName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblMRouteName.setText("Route");
        jpModifyPanel.add(jlblMRouteName);
        jlblMRouteName.setBounds(10, 20, 90, 30);

        jlblMRouteStatus.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblMRouteStatus.setText("Status");
        jpModifyPanel.add(jlblMRouteStatus);
        jlblMRouteStatus.setBounds(330, 20, 70, 30);

        jrbMRouteActive.setBackground(new java.awt.Color(255, 255, 255));
        jbgMStatus.add(jrbMRouteActive);
        jrbMRouteActive.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbMRouteActive.setSelected(true);
        jrbMRouteActive.setText("Active");
        jrbMRouteActive.setActionCommand("ACV");
        jrbMRouteActive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbMRouteActiveActionPerformed(evt);
            }
        });
        jpModifyPanel.add(jrbMRouteActive);
        jrbMRouteActive.setBounds(400, 20, 80, 30);

        jrbMRouteInactive.setBackground(new java.awt.Color(255, 255, 255));
        jbgMStatus.add(jrbMRouteInactive);
        jrbMRouteInactive.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbMRouteInactive.setText("Inactive");
        jrbMRouteInactive.setActionCommand("INV");
        jrbMRouteInactive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbMRouteInactiveActionPerformed(evt);
            }
        });
        jpModifyPanel.add(jrbMRouteInactive);
        jrbMRouteInactive.setBounds(490, 20, 80, 30);

        jlblARouteStatus3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblARouteStatus3.setText("Stations in current route");
        jpModifyPanel.add(jlblARouteStatus3);
        jlblARouteStatus3.setBounds(10, 60, 180, 30);
        jpModifyPanel.add(jSeparator4);
        jSeparator4.setBounds(10, 90, 180, 5);

        jspStationInRoute.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        jltStationinRoute.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        jspStationInRoute.setViewportView(jltStationinRoute);

        jpModifyPanel.add(jspStationInRoute);
        jspStationInRoute.setBounds(10, 95, 180, 180);

        jbtMAddStation.setBackground(new java.awt.Color(26, 188, 156));
        jbtMAddStation.setFont(new java.awt.Font("Segoe UI Emoji", 0, 11)); // NOI18N
        jbtMAddStation.setForeground(new java.awt.Color(255, 255, 255));
        jbtMAddStation.setToolTipText("Add to current route");
        jbtMAddStation.setFocusPainted(false);
        jbtMAddStation.setLabel("<<");
        jbtMAddStation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtMAddStationActionPerformed(evt);
            }
        });
        jpModifyPanel.add(jbtMAddStation);
        jbtMAddStation.setBounds(195, 130, 50, 20);

        jbtMRemoveStation.setBackground(new java.awt.Color(26, 188, 156));
        jbtMRemoveStation.setFont(new java.awt.Font("Segoe UI Emoji", 0, 11)); // NOI18N
        jbtMRemoveStation.setForeground(new java.awt.Color(255, 255, 255));
        jbtMRemoveStation.setToolTipText("Remove from current route");
        jbtMRemoveStation.setFocusPainted(false);
        jbtMRemoveStation.setLabel(">>");
        jbtMRemoveStation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtMRemoveStationActionPerformed(evt);
            }
        });
        jpModifyPanel.add(jbtMRemoveStation);
        jbtMRemoveStation.setBounds(195, 220, 50, 20);

        jspMAvaliableStation.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        jltMAvaliableStation.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        jspMAvaliableStation.setViewportView(jltMAvaliableStation);

        jpModifyPanel.add(jspMAvaliableStation);
        jspMAvaliableStation.setBounds(250, 95, 180, 180);

        jcbxRoute.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jcbxRoute.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbxRouteActionPerformed(evt);
            }
        });
        jpModifyPanel.add(jcbxRoute);
        jcbxRoute.setBounds(100, 20, 190, 30);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Rest of Stations");
        jpModifyPanel.add(jLabel1);
        jLabel1.setBounds(250, 60, 180, 30);
        jpModifyPanel.add(jSeparator5);
        jSeparator5.setBounds(250, 90, 180, 5);

        jbtStartSetTime1.setBackground(new java.awt.Color(39, 174, 96));
        jbtStartSetTime1.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtStartSetTime1.setForeground(new java.awt.Color(255, 255, 255));
        jbtStartSetTime1.setText("Set time");
        jbtStartSetTime1.setFocusPainted(false);
        jbtStartSetTime1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtStartSetTime1ActionPerformed(evt);
            }
        });
        jpModifyPanel.add(jbtStartSetTime1);
        jbtStartSetTime1.setBounds(235, 290, 150, 45);

        jbtSubmit1.setBackground(new java.awt.Color(39, 174, 96));
        jbtSubmit1.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtSubmit1.setForeground(new java.awt.Color(255, 255, 255));
        jbtSubmit1.setText("Submit");
        jbtSubmit1.setFocusPainted(false);
        jbtSubmit1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSubmit1ActionPerformed(evt);
            }
        });
        jpModifyPanel.add(jbtSubmit1);
        jbtSubmit1.setBounds(450, 290, 150, 45);

        jbtReset1.setBackground(new java.awt.Color(39, 174, 96));
        jbtReset1.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtReset1.setForeground(new java.awt.Color(255, 255, 255));
        jbtReset1.setText("Reset");
        jbtReset1.setFocusPainted(false);
        jbtReset1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtReset1ActionPerformed(evt);
            }
        });
        jpModifyPanel.add(jbtReset1);
        jbtReset1.setBounds(10, 290, 150, 45);

        jpModifyRouteCard.add(jpModifyPanel);
        jpModifyPanel.setBounds(0, 10, 605, 350);

        jpRouteCard.add(jpModifyRouteCard, "modifyroute");

        jpRouteOperation.add(jpRouteCard);
        jpRouteCard.setBounds(5, 55, 605, 360);

        jpRouteContent.add(jpRouteOperation);
        jpRouteOperation.setBounds(5, 5, 615, 420);

        jspListScroller.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        jltStationInRoute.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        jltStationInRoute.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jspListScroller.setViewportView(jltStationInRoute);

        jpRouteContent.add(jspListScroller);
        jspListScroller.setBounds(625, 240, 230, 185);

        jlblRoutes.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlblRoutes.setLabelFor(jltStationInRoute);
        jlblRoutes.setText("ROUTE");
        jlblRoutes.setToolTipText("");
        jpRouteContent.add(jlblRoutes);
        jlblRoutes.setBounds(625, 0, 230, 20);

        jlblStationInRoute.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlblStationInRoute.setLabelFor(jltStationInRoute);
        jlblStationInRoute.setText("STATION IN ROUTE");
        jlblStationInRoute.setToolTipText("");
        jpRouteContent.add(jlblStationInRoute);
        jlblStationInRoute.setBounds(625, 215, 230, 20);
        jpRouteContent.add(jSeparator2);
        jSeparator2.setBounds(625, 20, 230, 5);
        jpRouteContent.add(jSeparator3);
        jSeparator3.setBounds(625, 235, 230, 5);

        jpRoute.add(jpRouteContent);
        jpRouteContent.setBounds(5, 5, 860, 430);

        add(jpRoute);
        jpRoute.setBounds(0, 20, 870, 440);

        javax.swing.GroupLayout jpRouteArrowLayout = new javax.swing.GroupLayout(jpRouteArrow);
        jpRouteArrow.setLayout(jpRouteArrowLayout);
        jpRouteArrowLayout.setHorizontalGroup(
            jpRouteArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );
        jpRouteArrowLayout.setVerticalGroup(
            jpRouteArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        add(jpRouteArrow);
        jpRouteArrow.setBounds(180, 0, 150, 20);
    }// </editor-fold>//GEN-END:initComponents

    private void jtbRouteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbRouteMouseClicked
        if (jtbRoute.getSelectedRow() > -1) {
            JTable table = (JTable) evt.getSource();
            Object data = table.getModel().getValueAt(table.getSelectedRow(), 0);
            String result = RestServer.httpGet("route/station", data.toString());
            try {
                json = new JSONObject(result);
                boolean error = json.optBoolean("error");
                if (!error) {
                    int statusCode = json.getInt("status");
                    if (statusCode == 200) {
                        jsonArray = json.getJSONArray("content");
                        lstModel = new DefaultListModel();
                        for (int i = 0; i < jsonArray.length(); i++) {
                            lstModel.addElement(jsonArray.getJSONObject(i).getString("stationName"));
                        }
                        jltStationInRoute.setModel(lstModel);
                    }
                } else {
                    Toast.makeText(RoutePanel.this, json.getString("message"), Toast.LENGTH_LONG).display();
                }
            } catch (JSONException ex) {
            }
        }
    }//GEN-LAST:event_jtbRouteMouseClicked

    private void jbtAddRouteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAddRouteActionPerformed
        jbtAddRoute.setBackground(new Color(44, 62, 80));
        jbtModifyRoute.setBackground(new Color(52, 73, 94));
        jbtRemoveRoute.setBackground(new Color(52, 73, 94));
        populateAvailableStation();
        cl.show(jpRouteCard, "addroute");
    }//GEN-LAST:event_jbtAddRouteActionPerformed

    private void jbtModifyRouteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtModifyRouteActionPerformed
        jbtAddRoute.setBackground(new Color(52, 73, 94));
        jbtModifyRoute.setBackground(new Color(44, 62, 80));
        jbtRemoveRoute.setBackground(new Color(52, 73, 94));
        cl.show(jpRouteCard, "modifyroute");
        populateRouteComboBox();
        populateStationInRoute(1);
        populateRestOfStation(1);
        jpStationTime1.setVisible(false);
        if (stationTime != null) {
            stationTime.clear();
        }
        if (!stationTimes.isEmpty()) {
            stationTimes.clear();
        }
    }//GEN-LAST:event_jbtModifyRouteActionPerformed

    private void jbtRemoveRouteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtRemoveRouteActionPerformed
        jbtAddRoute.setBackground(new Color(52, 73, 94));
        jbtModifyRoute.setBackground(new Color(52, 73, 94));
        jbtRemoveRoute.setBackground(new Color(44, 62, 80));
        if (jtbRoute.getSelectedRow() > -1) {
            Object rid = jtbRoute.getModel().getValueAt(jtbRoute.getSelectedRow(), 0);
            String result = RestServer.httpDelete("route", rid.toString());
            try {
                json = new JSONObject(result);
                boolean error = json.optBoolean("error");
                if (!error) {
                    int statuscode = json.getInt("status");
                    if (statuscode == 200) {
                        Toast.makeText(this, "Route successfully deactivated.", Toast.LENGTH_SHORT).display();
                        populateTableData();
                    } else {
                        Toast.makeText(this, "This route has already deactivated.", Toast.LENGTH_SHORT).display();
                    }
                } else {
                    Toast.makeText(this, "Error occured, please try again later.", Toast.LENGTH_SHORT).display();
                }
            } catch (JSONException ex) {
            }
        } else {
            Toast.makeText(this, "Please make selection from the table first.", Toast.LENGTH_SHORT).display();
        }
    }//GEN-LAST:event_jbtRemoveRouteActionPerformed

    private void jbtStartSetTimeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtStartSetTimeActionPerformed
        jpStationTime.setVisible(true);
        jspArrivalTime.setEnabled(true);
        selectedIndex = 0;
        Station temp = (Station) jltStationToAdd.getModel().getElementAt(selectedIndex);
        stationTime = new HashMap<>();
        stationTime.put("sid", "" + temp.getSid());
        stationTime.put("order", "" + selectedIndex);
        jtfStationTime.setText(temp.getStationName());
    }//GEN-LAST:event_jbtStartSetTimeActionPerformed

    private void jbtAddStationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAddStationActionPerformed
        List selectedStation = jltAvailableStation.getSelectedValuesList();
        for (int i = 0; i < selectedStation.size(); i++) {
            lstAddModel.addElement((Station) selectedStation.get(i));
            lstStationModel.removeElement(selectedStation.get(i));
        }
        jbtStartSetTime.setEnabled(true);
    }//GEN-LAST:event_jbtAddStationActionPerformed

    private void jbtRemoveStationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtRemoveStationActionPerformed
        List selectedStation = jltStationToAdd.getSelectedValuesList();
        for (int i = 0; i < selectedStation.size(); i++) {
            Station temp = (Station) selectedStation.get(i);
            lstStationModel.add(temp.getOrder(), temp);
            lstAddModel.removeElement(selectedStation.get(i));
        }
        jpStationTime.setVisible(false);
        if (jltStationToAdd.getModel().getSize() == 0) {
            jbtStartSetTime.setEnabled(false);
        }
        resetTimeEditing();
    }//GEN-LAST:event_jbtRemoveStationActionPerformed

    private void jbtSetTimeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSetTimeActionPerformed
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        boolean contain = false;
        boolean timeconflict = false;
        Date tempTime = new Date();
        if (!stationTimes.isEmpty()) {
            for (int i = 0; i < stationTimes.size(); i++) {
                Map<String, String> map = stationTimes.get(i);
                Station temp = (Station) jltStationToAdd.getModel().getElementAt(selectedIndex);
                try {
                    tempTime = format.parse(map.get("time"));
                } catch (ParseException ex) {
                }
                int compare = ((Date) jspArrivalTime.getModel().getValue()).compareTo(tempTime);
                if (compare <= 0) {
                    Toast.makeText(this, "Cannot select the same or lesser time", Toast.LENGTH_SHORT).display();
                    timeconflict = true;
                    contain = false;
                    break;
                } else {
                    if (map.get("sid").equals("" + temp.getSid())) {
                        contain = true;
                        timeconflict = false;
                        stationTime = null;
                        break;
                    }
                }
            }
        }
        if (!contain && !timeconflict) {
            stationTime.put("time", format.format(jspArrivalTime.getModel().getValue()));
            stationTimes.add(stationTime);
            if ((selectedIndex + 1) < jltStationToAdd.getModel().getSize()) {
                selectedIndex++;
                Station temp = (Station) jltStationToAdd.getModel().getElementAt(selectedIndex);
                stationTime = new HashMap<>();
                stationTime.put("sid", "" + temp.getSid());
                stationTime.put("order", "" + selectedIndex);
                jtfStationTime.setText(temp.getStationName());
            } else {
                jspArrivalTime.setEnabled(false);
                jbtSetTime.setEnabled(false);
                jbtStartSetTime.setEnabled(false);
            }
        }
    }//GEN-LAST:event_jbtSetTimeActionPerformed

    private void jbtResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtResetActionPerformed
        jtfStationTime.setText(null);
        jtfARouteName.setText(null);
        jrbARouteActive.setSelected(true);
        jspArrivalTime.setValue(initDate);
        populateAvailableStation();
        jpStationTime.setVisible(false);
        if (stationTime != null) {
            stationTime.clear();
        }
        if (!stationTimes.isEmpty()) {
            stationTimes.clear();
        }
        jbtStartSetTime.setEnabled(false);
        resetTimeEditing();
    }//GEN-LAST:event_jbtResetActionPerformed

    private void jbtSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSubmitActionPerformed
        if (jtfARouteName.getText().equals("")
                || jltStationToAdd.getModel().getSize() == 0
                || stationTimes.isEmpty()
                || stationTimes.size() != jltStationToAdd.getModel().getSize()) {
            Toast.makeText(this, "Please fill in all field first.", Toast.LENGTH_SHORT).display();
        } else {
            Pattern pattern = Pattern.compile("^[A-Za-z]{1,}-[A-Za-z]{1,}$");
            Matcher match = pattern.matcher(jtfARouteName.getText());
            if (!match.matches()) {
                Toast.makeText(this, "Route name must contain alphabets only, separated by a dash, eg. IPOH-KL", Toast.LENGTH_SHORT).display();
            } else {
                int option = JOptionPane.showConfirmDialog(this,
                        "Do you want to add this route?", "Confirmation",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (option == JOptionPane.YES_OPTION) {
                    Map<String, Object> data = new HashMap<>();
                    String routename = jtfARouteName.getText();
                    data.put("froute", routename);
                    data.put("broute", routename.substring(routename.lastIndexOf("-") + 1) + "-" + routename.substring(0, routename.lastIndexOf("-")));
                    data.put("status", status);
                    data.put("fstation", buildJson(stationTimes, MapSortHelper.SORT_ASC));
                    data.put("bstation", buildJson(stationTimes, MapSortHelper.SORT_DSC));
                    String result = RestServer.httpPost("route", data);
                    try {
                        json = new JSONObject(result);
                        boolean error = json.optBoolean("error");
                        if (!error) {
                            int statuscode = json.getInt("status");
                            if (statuscode == 200) {
                                Toast.makeText(this, json.getString("content"), Toast.LENGTH_SHORT).display();
                                populateTableData();
                            } else {
                                Toast.makeText(this, json.getString("content"), Toast.LENGTH_SHORT).display();
                            }
                        }
                    } catch (JSONException ex) {
                    }
                }
            }
        }
    }//GEN-LAST:event_jbtSubmitActionPerformed

    private void jrbARouteActiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbARouteActiveActionPerformed
        status = evt.getActionCommand();
    }//GEN-LAST:event_jrbARouteActiveActionPerformed

    private void jrbARouteInactiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbARouteInactiveActionPerformed
        status = evt.getActionCommand();
    }//GEN-LAST:event_jrbARouteInactiveActionPerformed

    private void jrbMRouteActiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbMRouteActiveActionPerformed
        status = evt.getActionCommand();
    }//GEN-LAST:event_jrbMRouteActiveActionPerformed

    private void jrbMRouteInactiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbMRouteInactiveActionPerformed
        status = evt.getActionCommand();
    }//GEN-LAST:event_jrbMRouteInactiveActionPerformed

    private void jbtMAddStationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtMAddStationActionPerformed
        List selectedStation = jltMAvaliableStation.getSelectedValuesList();
        for (int i = 0; i < selectedStation.size(); i++) {
            dlmSIR.addElement((Station) selectedStation.get(i));
            dlmROS.removeElement(selectedStation.get(i));
        }
    }//GEN-LAST:event_jbtMAddStationActionPerformed

    private void jbtMRemoveStationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtMRemoveStationActionPerformed
        List selectedStation = jltStationinRoute.getSelectedValuesList();
        for (int i = 0; i < selectedStation.size(); i++) {
            Station temp = (Station) selectedStation.get(i);
            if (temp.getOrder() > 0) {
                dlmROS.add(temp.getOrder(), temp);
            } else {
                dlmROS.add(0, temp);
            }
            dlmSIR.removeElement(selectedStation.get(i));
        }
    }//GEN-LAST:event_jbtMRemoveStationActionPerformed

    private void jcbxRouteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbxRouteActionPerformed
        int rid = ((Route) jcbxRoute.getSelectedItem()).getRid();
        String stat = ((Route) jcbxRoute.getSelectedItem()).getStatus();
        if (stat.equals("ACV")) {
            jrbMRouteActive.setSelected(true);
        } else {
            jrbMRouteInactive.setSelected(true);
        }
        populateStationInRoute(rid);
        populateRestOfStation(rid);
    }//GEN-LAST:event_jcbxRouteActionPerformed

    private void jbtStartSetTime1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtStartSetTime1ActionPerformed
        jpStationTime1.setVisible(true);
        jbtSetTime1.setEnabled(true);
        jspArrivalTime1.setEnabled(true);
        selectedIndex = 0;
        Station temp = (Station) jltStationinRoute.getModel().getElementAt(selectedIndex);
        stationTime = new HashMap<>();
        stationTime.put("sid", "" + temp.getSid());
        stationTime.put("order", "" + selectedIndex);
        jtfStationTime1.setText(temp.getStationName());
    }//GEN-LAST:event_jbtStartSetTime1ActionPerformed

    private void jbtSubmit1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSubmit1ActionPerformed
        Map<String, Object> data = new HashMap<>();
        String result;
        if (stationTimes.isEmpty()) {
            Route temp = (Route) jcbxRoute.getSelectedItem();
            data.put("routeName", temp.getRouteName());
            data.put("status", status);
            result = RestServer.httpPut("route", data, "" + temp.getRid());
            System.out.println(result);
        } else {
            Route temp = (Route) jcbxRoute.getSelectedItem();
            data.put("routeName", temp.getRouteName());
            data.put("status", status);
            data.put("stations", buildJson(stationTimes));
            result = RestServer.httpPut("route/station", data, "" + temp.getRid());
            System.out.println(result);
        }
        try {
            json = new JSONObject(result);
            boolean error = json.optBoolean("error");
            if (!error) {
                int statuscode = json.getInt("status");
                if (statuscode == 200) {
                    Toast.makeText(this, "Route has been updated", Toast.LENGTH_SHORT).display();
                    populateRouteComboBox();
                    populateTableData();
                } else {
                    Toast.makeText(this, json.getString("content"), Toast.LENGTH_SHORT).display();
                }
            } else {
                Toast.makeText(this, json.getString("message"), Toast.LENGTH_SHORT).display();
            }
        } catch (JSONException ex) {
        }
    }//GEN-LAST:event_jbtSubmit1ActionPerformed

    private void jbtSetTime1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSetTime1ActionPerformed
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        boolean contain = false;
        boolean timeconflict = false;
        Date tempTime = new Date();
        if (!stationTimes.isEmpty()) {
            for (int i = 0; i < stationTimes.size(); i++) {
                Map<String, String> map = stationTimes.get(i);
                Station temp = (Station) jltStationinRoute.getModel().getElementAt(selectedIndex);
                try {
                    tempTime = format.parse(map.get("time"));
                } catch (ParseException ex) {
                }
                int compare = ((Date) jspArrivalTime1.getModel().getValue()).compareTo(tempTime);
                if (compare <= 0) {
                    Toast.makeText(this, "Cannot select the same or lesser time", Toast.LENGTH_SHORT).display();
                    timeconflict = true;
                    contain = false;
                    break;
                } else {
                    if (map.get("sid").equals("" + temp.getSid())) {
                        contain = true;
                        timeconflict = false;
                        stationTime = null;
                        break;
                    }
                }
            }
        }
        if (!contain && !timeconflict) {
            stationTime.put("time", format.format(jspArrivalTime1.getModel().getValue()));
            stationTimes.add(stationTime);
            if ((selectedIndex + 1) < jltStationinRoute.getModel().getSize()) {
                selectedIndex++;
                Station temp = (Station) jltStationinRoute.getModel().getElementAt(selectedIndex);
                stationTime = new HashMap<>();
                stationTime.put("sid", "" + temp.getSid());
                stationTime.put("order", "" + selectedIndex);
                jtfStationTime1.setText(temp.getStationName());
            } else {
                jspArrivalTime1.setEnabled(false);
                jbtSetTime1.setEnabled(false);
                jbtStartSetTime1.setEnabled(false);
            }
        }
    }//GEN-LAST:event_jbtSetTime1ActionPerformed

    private void jbtReset1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtReset1ActionPerformed
        modifyReset();
    }//GEN-LAST:event_jbtReset1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.ButtonGroup jbgAStatus;
    private javax.swing.ButtonGroup jbgMStatus;
    private javax.swing.JButton jbtAddRoute;
    private javax.swing.JButton jbtAddStation;
    private javax.swing.JButton jbtMAddStation;
    private javax.swing.JButton jbtMRemoveStation;
    private javax.swing.JButton jbtModifyRoute;
    private javax.swing.JButton jbtRemoveRoute;
    private javax.swing.JButton jbtRemoveStation;
    private javax.swing.JButton jbtReset;
    private javax.swing.JButton jbtReset1;
    private javax.swing.JButton jbtSetTime;
    private javax.swing.JButton jbtSetTime1;
    private javax.swing.JButton jbtStartSetTime;
    private javax.swing.JButton jbtStartSetTime1;
    private javax.swing.JButton jbtSubmit;
    private javax.swing.JButton jbtSubmit1;
    private javax.swing.JComboBox jcbxRoute;
    private javax.swing.JLabel jlblARouteError;
    private javax.swing.JLabel jlblARouteName;
    private javax.swing.JLabel jlblARouteStatus;
    private javax.swing.JLabel jlblARouteStatus1;
    private javax.swing.JLabel jlblARouteStatus3;
    private javax.swing.JLabel jlblArrivalTime;
    private javax.swing.JLabel jlblArrivalTime1;
    private javax.swing.JLabel jlblMRouteName;
    private javax.swing.JLabel jlblMRouteStatus;
    private javax.swing.JLabel jlblRoutes;
    private javax.swing.JLabel jlblSetTime;
    private javax.swing.JLabel jlblSetTime1;
    private javax.swing.JLabel jlblStationInRoute;
    private javax.swing.JList jltAvailableStation;
    private javax.swing.JList jltMAvaliableStation;
    private javax.swing.JList jltStationInRoute;
    private javax.swing.JList jltStationToAdd;
    private javax.swing.JList jltStationinRoute;
    private javax.swing.JPanel jpAddArrow;
    private javax.swing.JPanel jpAddPanel;
    private javax.swing.JPanel jpAddRouteCard;
    private javax.swing.JPanel jpEditStationTime;
    private javax.swing.JPanel jpEditStationTime1;
    private javax.swing.JPanel jpModifyArrow;
    private javax.swing.JPanel jpModifyPanel;
    private javax.swing.JPanel jpModifyRouteCard;
    private javax.swing.JPanel jpNullCard;
    private javax.swing.JPanel jpRoute;
    private javax.swing.JPanel jpRouteArrow;
    private javax.swing.JPanel jpRouteCard;
    private javax.swing.JPanel jpRouteContent;
    private javax.swing.JPanel jpRouteOperation;
    private javax.swing.JPanel jpStationTime;
    private javax.swing.JPanel jpStationTime1;
    private javax.swing.JPanel jpVerticalArrow;
    private javax.swing.JPanel jpVerticalArrow1;
    private javax.swing.JRadioButton jrbARouteActive;
    private javax.swing.JRadioButton jrbARouteInactive;
    private javax.swing.JRadioButton jrbMRouteActive;
    private javax.swing.JRadioButton jrbMRouteInactive;
    private javax.swing.JSpinner jspArrivalTime;
    private javax.swing.JSpinner jspArrivalTime1;
    private javax.swing.JScrollPane jspAvaliableScroller;
    private javax.swing.JScrollPane jspListScroller;
    private javax.swing.JScrollPane jspMAvaliableStation;
    private javax.swing.JScrollPane jspStationInRoute;
    private javax.swing.JScrollPane jspStationToAdd;
    private javax.swing.JScrollPane jspTableScroller;
    private javax.swing.JTable jtbRoute;
    private javax.swing.JTextField jtfARouteName;
    private javax.swing.JTextField jtfStationTime;
    private javax.swing.JTextField jtfStationTime1;
    // End of variables declaration//GEN-END:variables
}
